<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProductCategorySide
 *
 * @author ptu
 */
class ProductCategorySide extends CWidget {
    
    public $searchUrl; //ex: product/category/$id
    public $selectedId;
    private $baseAssetUrl;
    
    public function init(){
        $this->registerAssets();
        
        $model=new ProductCategory;

        $treeData=$model->getTreeDataAndExpandLevel($this->selectedId);

        echo '<div id="'.$this->id.'">';
        $this->widget('CTreeView', array(
            'id'=>'category-tree',
            'data'=>$treeData['nodes'],
            'animated'=>'normal',
            'collapsed'=>true,
            'cssFile'=>$this->baseAssetUrl . '/jquery.treeview.css',
            'htmlOptions'=>array(
                'class'=>'treeview-famfamfam filetree'
            )
        ));
        echo '</div>';
        
        $cs=Yii::app()->clientScript;
        $id=$this->id;
        
        $options=array(
            'searchUrl'=>  $this->searchUrl,
            'selectedId'=>$this->selectedId
        );
        
        $options=CJavaScript::encode($options);
        
        $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').productCategorySide($options);");
    } 
    
    
    private function registerAssets(){
        $this->baseAssetUrl=Yii::app()->getAssetManager()->publish(__DIR__ . DIRECTORY_SEPARATOR . 'assets');
        
        $cs=Yii::app()->clientScript;
        
        $cs->registerCssFile($this->baseAssetUrl . '/product_category_side.css');
        $cs->registerScriptFile($this->baseAssetUrl . '/product_category_side.js', CClientScript::POS_END);
    }
}

?>
